package index.query;

import compiler.absyn.Constant;
import engine.query.Scan;
import engine.query.TableScan;

import storage.record.RID;


public class LookupScan implements Scan {
	private RidScan rs;
	private TableScan ts;


	public LookupScan(RidScan rs, TableScan ts) {
		this.rs = rs;
		this.ts = ts;
		beforeFirst();
	}

	
	public void beforeFirst() {
		rs.beforeFirst();
	}


	public boolean next() {
		boolean ok = rs.next();
		if (ok) {
			RID rid = rs.getDataRid();
			ts.moveToRid(rid);
		}
		return ok;
	}

	public void close() {
		rs.close();
		ts.close();
	}


	public Constant getVal(String fldname) {
		if (ts.hasField(fldname))
			return ts.getVal(fldname);
		else
			return rs.getVal(fldname);
	}


	public int getInt(String fldname) {
		if (ts.hasField(fldname))
			return ts.getInt(fldname);
		else
			return rs.getInt(fldname);
	}


	public String getString(String fldname) {
		if (ts.hasField(fldname))
			return ts.getString(fldname);
		else
			return rs.getString(fldname);
	}


	public boolean hasField(String fldname) {
		return ts.hasField(fldname) || rs.hasField(fldname);
	}
}
